package com.ruoyi.system.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.processor.OssProcessor;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.service.IRoomService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 房间管理Controller
 * 
 * @author zdk
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/qipaishi/room")
@Slf4j
public class RoomController extends BaseController
{
    @Autowired
    private IRoomService roomService;

    @Resource
    private OssProcessor ossProcessor;

    /**
     * 查询房间管理列表
     */
    @PreAuthorize("@ss.hasPermi('qipaishi:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(Room room)
    {
        startPage();
        List<Room> list = roomService.selectRoomList(room);
        return getDataTable(list);
    }

    /**
     * 导出房间管理列表
     */
    @PreAuthorize("@ss.hasPermi('qipaishi:room:export')")
    @Log(title = "房间管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Room room)
    {
        List<Room> list = roomService.selectRoomList(room);
        ExcelUtil<Room> util = new ExcelUtil<Room>(Room.class);
        util.exportExcel(response, list, "房间管理数据");
    }

    /**
     * 获取房间管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('qipaishi:room:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(roomService.selectRoomById(id));
    }

    /**
     * 新增房间管理
     */
    @PreAuthorize("@ss.hasPermi('qipaishi:room:add')")
    @Log(title = "房间管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Room room)
    {
        return toAjax(roomService.insertRoom(room));
    }

    /**
     * 修改房间管理
     */
    @PreAuthorize("@ss.hasPermi('qipaishi:room:edit')")
    @Log(title = "房间管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Room room)
    {
        return toAjax(roomService.updateRoom(room));
    }

    /**
     * 删除房间管理
     */
    @PreAuthorize("@ss.hasPermi('qipaishi:room:remove')")
    @Log(title = "房间管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(roomService.deleteRoomByIds(ids));
    }

    @Log(title = "房间图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return error("上传文件不能为空");
        }

        InputStream inputStream = null;
        try {
            // 生成文件名
            String fileName = FileUploadUtils.extractFilename(file);

            // 使用 try-with-resources 确保流自动关闭
            inputStream = file.getInputStream();

            // 调用 ossProcessor 上传文件
            String url = ossProcessor.uploadFile(fileName, inputStream);
            AjaxResult ajax = AjaxResult.success();
            // 返回上传结果
            return  ajax.put("imageUrl", url);
        } catch (IOException e) {
            return error("上传图片失败，请重试");
        } catch (Exception e) {
            return error("上传图片异常，请联系管理员");
        } finally {
            // 确保输入流关闭，避免临时文件被占用
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.warn("关闭输入流失败", e);
                }
            }
        }
    }
}
